//
//  CacheData.m
//  KidReading
//
//  Created by telen on 14/12/4.
//  Copyright (c) 2014年 刘赞黄Telen. All rights reserved.
//

#import "CacheData.h"
#import "ConstDefine.h"
typedef long long LongLong;

#if __Debug_CloseAES == 0
#if Net == 1
#define __JRUser @"JRUser_lan3.1.telen"
#define __FileNetCache @"TLNetCache_lan.telen"
#define __FileBookCache @"TLBooksCache_lan.telen"
#define __FileTalkCache @"TLTalkCache_lan.telen"
#define __FileTalkVideoCache @"TLTalkVideoCache_lan.telen"
#elif Net == 2
#define __JRUser @"JRUser_wlantest3.1.telen"
#define __FileNetCache @"TLNetCache_wlantest.telen"
#define __FileBookCache @"TLBooksCache_wlantest.telen"
#define __FileTalkCache @"TLTalkCache_wlantest.telen"
#define __FileTalkVideoCache @"TLTalkVideoCache_wlantest.telen"
#elif Net == 3
#define __JRUser @"JRUser3.1.telen"
#define __FileNetCache @"TLNetCache.telen"
#define __FileBookCache @"TLBooksCache.telen"
#define __FileTalkCache @"TLTalkCache.telen"
#define __FileTalkVideoCache @"TLTalkVideoCache.telen"
#elif Net == 4
#define __JRUser @"JRUser_devlan3.1.telen"
#define __FileNetCache @"TLNetCache_devlan.telen"
#define __FileBookCache @"TLBooksCache_devlan.telen"
#define __FileTalkCache @"TLTalkCache_devlan.telen"
#define __FileTalkVideoCache @"TLTalkVideoCache_devlan.telen"
#endif

#else
#if Net == 1
#define __JRUser @"JRUser_lan.txt"
#define __FileNetCache @"TLNetCache_lan.txt"
#define __FileBookCache @"TLBooksCache_lan.txt"
#define __FileTalkCache @"TLTalkCache_lan.txt"
#define __FileTalkVideoCache @"TLTalkVideoCache_lan.txt"
#elif Net == 2
#define __JRUser @"JRUser_wlantest.txt"
#define __FileNetCache @"TLNetCache_wlantest.txt"
#define __FileBookCache @"TLBooksCache_wlantest.txt"
#define __FileTalkCache @"TLTalkCache_wlantest.txt"
#define __FileTalkVideoCache @"TLTalkVideoCache_wlantest.txt"
#elif Net == 3
#define __JRUser @"JRUser.txt"
#define __FileNetCache @"TLNetCache.txt"
#define __FileBookCache @"TLBooksCache.txt"
#define __FileTalkCache @"TLTalkCache.txt"
#define __FileTalkVideoCache @"TLTalkVideoCache.txt"
#elif Net == 4
#define __JRUser @"JRUser_devlan.txt"
#define __FileNetCache @"TLNetCache_devlan.txt"
#define __FileBookCache @"TLBooksCache_devlan.txt"
#define __FileTalkCache @"TLTalkCache_devlan.txt"
#define __FileTalkVideoCache @"TLTalkVideoCache_devlan.txt"
#endif

#endif

#define __BookDownOrderArr @"bookDownOrder"
#define __TalkDownOrderArr @"talkDownOrder"
#define __BookDownIng @"bookDownIng"
#define __TalkDownIng @"talkDownIng"
#define __TalkVideoDownIng @"talkVideoDownIng"
#define __BookOpenRecord @"bookOpenRecords"
#define __TalkOpenRecord @"talkOpenRecords"
#define __FlowBookOpenRecrod @"flowBookOpenRecord"
#define __BookListPaths @"bookListPaths"

#define loginInfo @"loginInfo"//不在区分2.0,让1.0用户再login


#pragma mark- CacheData

@implementation CacheData

static CacheData* cacheDataSingle = nil;

+ (CacheData *)shareSingle
{
    if (!cacheDataSingle) {
        cacheDataSingle = [[CacheData alloc] init];
    }
    return cacheDataSingle;
}

- (id)init
{
    if (self = [super init]) {
        NSString *documentsDirectory = [self getSavePath];
        NSString *SystemSave = [documentsDirectory stringByAppendingPathComponent:__FileNetCache];
#if __Debug_CloseAES == 0
        NSData* data = [[NSData alloc] initWithContentsOfFile:SystemSave];
        data = [data AES256DecryptWithKey:__AESKey];//解密
        @try {
            netCache = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        @catch (NSException *exception) {
            NSLog(@"%@",exception);
        }
        @finally {
            
        }
        
#else
        netCache = [[NSMutableDictionary alloc] initWithContentsOfFile:SystemSave];
#endif
        if (!netCache) {
            netCache = [[NSMutableDictionary alloc] initWithCapacity:0];
        }
        //
        SystemSave = [documentsDirectory stringByAppendingPathComponent:__FileBookCache];
#if __Debug_CloseAES == 0
        data = [[NSData alloc] initWithContentsOfFile:SystemSave];
        data = [data AES256DecryptWithKey:__AESKey];//解密
        @try {
            NSDictionary *sbookCache = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            if (sbookCache) {
               bookCache = [sbookCache mutableCopy];
            }
        }
        @catch (NSException *exception) {
            NSLog(@"%@",exception);
        }
        @finally {
            
        }
#else
        bookCache = [[NSMutableDictionary alloc] initWithContentsOfFile:SystemSave];
#endif
        if (!bookCache) {
            bookCache = [[NSMutableDictionary alloc] initWithCapacity:0];
        }
        
        //
        SystemSave = [documentsDirectory stringByAppendingPathComponent:__FileTalkCache];
#if __Debug_CloseAES == 0
        data = [[NSData alloc] initWithContentsOfFile:SystemSave];
        data = [data AES256DecryptWithKey:__AESKey];//解密
        @try {
            talkCache = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        @catch (NSException *exception) {
            NSLog(@"%@",exception);
        }
        @finally {
            
        }
#else
        talkCache = [[NSMutableDictionary alloc] initWithContentsOfFile:SystemSave];
#endif

        if (!talkCache) {
            talkCache = [[NSMutableDictionary alloc] initWithCapacity:0];
        }
        
        //talkVideoEdition
        SystemSave = [documentsDirectory stringByAppendingPathComponent:__FileTalkVideoCache];
#if __Debug_CloseAES == 0
        data = [[NSData alloc] initWithContentsOfFile:SystemSave];
        data = [data AES256DecryptWithKey:__AESKey];//解密
        @try {
            talkVideoCache = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        @catch (NSException *exception) {
            NSLog(@"%@",exception);
        }
        @finally {
            
        }
#else
        talkVideoCache = [[NSMutableDictionary alloc] initWithContentsOfFile:SystemSave];
#endif
        
        if (!talkVideoCache) {
            talkVideoCache = [[NSMutableDictionary alloc] initWithCapacity:0];
        }
        
        
#if __Debug_File
        NSLog(@"File_Path: %@",SystemSave);
        NSLog(@"File_netCache = %@",netCache);
#endif
    }
    return self;
}

- (NSString*)getSavePath
{
    NSArray  *paths              = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

- (void)saveNetCache
{
    [DataProcessing do_Async_quick:^{
        NSString *documentsDirectory = [self getSavePath];
        NSString *SystemSave         = [documentsDirectory stringByAppendingPathComponent:__FileNetCache];
        @try {
#if __Debug_CloseAES == 0
            NSData* data = [NSKeyedArchiver archivedDataWithRootObject:netCache];
            data = [data AES256EncryptWithKey:__AESKey];//加密
            [data writeToFile:SystemSave atomically:YES];
#else
            [netCache writeToFile:SystemSave atomically:YES];
#endif
        } @catch (NSException *exception) {
            NSLog(@"saveNetCache Error: %@",exception);
        } @finally {
            
        }
    }];
}

- (void)saveBookCache
{
    //[DataProcessing do_Async_quick:^{
        NSString *documentsDirectory = [self getSavePath];
        NSString *SystemSave         = [documentsDirectory stringByAppendingPathComponent:__FileBookCache];
        @try {
#if __Debug_CloseAES == 0
            NSDictionary *sbookCache = nil;
            if (bookCache) {
               sbookCache = [NSDictionary dictionaryWithDictionary:bookCache];
            }
            if (sbookCache) {
                NSData* data = [NSKeyedArchiver archivedDataWithRootObject:sbookCache];
                data = [data AES256EncryptWithKey:__AESKey];//加密
                [data writeToFile:SystemSave atomically:YES];
            }
#else
            [bookCache writeToFile:SystemSave atomically:YES];
#endif
        } @catch (NSException *exception) {
            NSLog(@"saveBookCache Error: %@",exception);
        } @finally {
            
        }
   // }];
}

- (void)saveTalkCache
{
//    [DataProcessing do_Async_quick:^{
        NSString *documentsDirectory = [self getSavePath];
        NSString *SystemSave         = [documentsDirectory stringByAppendingPathComponent:__FileTalkCache];
        @try {
#if __Debug_CloseAES == 0
            NSData* data = [NSKeyedArchiver archivedDataWithRootObject:talkCache];
            data = [data AES256EncryptWithKey:__AESKey];//加密
            [data writeToFile:SystemSave atomically:YES];
#else
            [talkCache writeToFile:SystemSave atomically:YES];
#endif
        } @catch (NSException *exception) {
            NSLog(@"saveTalkCache Error: %@",exception);
        } @finally {
            
        }
//    }];
}

- (void)saveTalkVideoCache
{
//    [DataProcessing do_Async_quick:^{
        NSString *documentsDirectory = [self getSavePath];
        NSString *SystemSave         = [documentsDirectory stringByAppendingPathComponent:__FileTalkVideoCache];
        @try {
#if __Debug_CloseAES == 0
            NSData* data = [NSKeyedArchiver archivedDataWithRootObject:talkVideoCache];
            data = [data AES256EncryptWithKey:__AESKey];//加密
            [data writeToFile:SystemSave atomically:YES];
#else
            [talkVideoCache writeToFile:SystemSave atomically:YES];
#endif
        } @catch (NSException *exception) {
            NSLog(@"saveTalkVideoCache Error: %@",exception);
        } @finally {
            
        }
//    }];
}



- (void)updateNetCache:(NSDictionary *)dict forKey:(NSString *)key
{
    if(dict)[netCache setObject:dict forKey:key];
    [self saveNetCache];
}

- (NSDictionary *)getNetCacheForKey:(NSString *)key
{
    return [netCache objectForKey:key];
}

- (NSDictionary *)getNetCacheForBookID:(NSInteger)bookID withHomeCacheKey:(NSString *)homeKey
{
    NSDictionary* dict = [self getNetCacheForKey:homeKey];
    if (dict) {
        NSArray* allGrids = dict.allKeys;
        for (NSString* key in allGrids) {
            if(key.length==0)continue;
            NSArray* arr = [dict objectForKey:key];
            for (NSDictionary* dic in arr) {
                NSInteger bi = [[dic objectForKey:@"id"] integerValue];
                if (bi == bookID) {
                    return dic;
                }
            }
        }
    }
    return nil;
}

- (NSArray *)getNetCaheForGridID:(NSInteger)grid withHomeCacheKey:(NSString *)homeKey
{
    NSDictionary* dict = [self getNetCacheForKey:homeKey];
    if (dict) {
        return [dict objectForKey:[NSString stringWithFormat:@"%@",@(grid)]];
    }
    return nil;
}

- (void)updateNetCache:(NSDictionary *)dict forKey:(NSString *)key forUid:(NSInteger)uid
{
    NSString* ky = [NSString stringWithFormat:@"%@__%@",@(uid),key];
    if(dict)[netCache setObject:dict forKey:ky];
    [self saveNetCache];
}

- (NSDictionary *)getNetCacheForKey:(NSString *)key forUid:(NSInteger)uid
{
    NSString* ky = [NSString stringWithFormat:@"%@__%@",@(uid),key];
    return [netCache objectForKey:ky];
}

- (NSDictionary *)getNetCacheForBookID:(NSInteger)bookID withHomeCacheKey:(NSString *)homeKey forUid:(NSInteger)uid
{
    NSDictionary* dict = [self getNetCacheForKey:homeKey forUid:uid];
    if (dict) {
        NSArray* allGrids = dict.allKeys;
        for (NSString* key in allGrids) {
            if(key.length==0)continue;
            NSArray* arr = [dict objectForKey:key];
            for (NSDictionary* dic in arr) {
                NSInteger bi = [[dic objectForKey:@"id"] integerValue];
                if (bi == bookID) {
                    return dic;
                }
            }
        }
    }
    return nil;
}

- (NSArray *)getNetCaheForGridID:(NSInteger)grid withHomeCacheKey:(NSString *)homeKey forUid:(NSInteger)uid
{
    NSDictionary* dict = [self getNetCacheForKey:homeKey forUid:uid];
    if (dict) {
        return [dict objectForKey:[NSString stringWithFormat:@"%@",@(grid)]];
    }
    return nil;
}

#pragma mark BookCache
- (void)updateBook:(NSInteger)bookid CacheEdition:(long long)edition
{
    NSString* key = [NSString stringWithFormat:@"%@",@(bookid)];
    [bookCache setObject:[NSNumber numberWithLongLong:edition] forKey:key];
    NSMutableArray* marr = [[NSMutableArray alloc] initWithArray:[bookCache objectForKey:__BookDownOrderArr]];
    BOOL needAdd = YES;
    for (NSString* ky in marr) {
        if(ky.length==0)continue;
        if ([key isEqual:ky]) {
            needAdd = NO;
            break;
        }
    }
    if(needAdd)[marr addObject:key];
    [bookCache setObject:marr forKey:__BookDownOrderArr];
    [self saveBookCache];
}

- (void)updateBook:(NSInteger)bookid CacheEdition:(long long)edition forUid:(NSInteger)uid
{
    NSString* key = [NSString stringWithFormat:@"%@",@(bookid)];
    NSString* kys = [NSString stringWithFormat:@"%@__%@",@(uid),key];
    [bookCache setObject:[NSNumber numberWithLongLong:edition] forKey:kys];
    //
    NSString* ky_order = [NSString stringWithFormat:@"%d__%@",(int)uid,__BookDownOrderArr];
    NSMutableArray* marr = [[NSMutableArray alloc] initWithArray:[bookCache objectForKey:ky_order]];
    BOOL needAdd = YES;
    for (NSString* ky in marr) {
        if(ky.length==0)continue;
        if ([key isEqual:ky]) {
            needAdd = NO;
            break;
        }
    }
    if(needAdd)[marr addObject:key];
    [bookCache setObject:marr forKey:ky_order];
    [self saveBookCache];
}

- (void)updateBook:(NSInteger)bookid DownEdition:(long long)edition
{
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(bookid),__BookDownIng];
    [bookCache setObject:[NSNumber numberWithLongLong:edition] forKey:key];
    [self saveBookCache];
}

- (void)updateBook:(NSInteger)bookid OpenPage:(NSInteger)page forUid:(NSInteger)uid
{
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(bookid),__BookOpenRecord];
    NSString* ky = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
    [bookCache setObject:[NSNumber numberWithInteger:page] forKey:ky];
    [self saveBookCache];
}

- (void)deleteBookCache:(NSInteger)bookid forUid:(NSInteger)uid
{
    //移除书本下载记录 edition
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(bookid),__BookDownIng];
    id obj = [bookCache objectForKey:key];
    if (obj) {
        [bookCache removeObjectForKey:key];
    }
    //移除书本已下载记录 edition
    key = [NSString stringWithFormat:@"%@",@(bookid)];
    obj = [bookCache objectForKey:key];
    if (obj) {
        [bookCache removeObjectForKey:key];
    }
    //移除书本下载顺序中记录
    NSMutableArray* marr = [[NSMutableArray alloc] initWithArray:[bookCache objectForKey:__BookDownOrderArr]];
    NSString* objkey = nil;
    for (NSString* ky in marr) {
        if(ky.length==0)continue;
        if ([key isEqual:ky]) {
            objkey = ky;
            break;
        }
    }
    if(objkey)[marr removeObject:objkey];
    [bookCache setObject:marr forKey:__BookDownOrderArr];
    //移除阅读记录
    key = [NSString stringWithFormat:@"%@_%@",@(bookid),__BookOpenRecord];
    NSString* ky = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
    obj = [bookCache objectForKey:ky];
    if (obj) {
        [bookCache removeObjectForKey:ky];
    }
    //移除用户的下载记录
    key = [NSString stringWithFormat:@"%@",@(bookid)];
    NSString* kys = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
    obj = [bookCache objectForKey:kys];
    if (obj) {
        [bookCache removeObjectForKey:kys];
    }
    //移除用的下载顺序
    NSString* ky_order = [NSString stringWithFormat:@"%d__%@",(int)uid,__BookDownOrderArr];
    marr = [[NSMutableArray alloc] initWithArray:[bookCache objectForKey:ky_order]];
    objkey = nil;
    for (NSString* ky in marr) {
        if(ky.length==0)continue;
        if ([key isEqual:ky]) {
            objkey = ky;
            break;
        }
    }
    if(objkey)[marr removeObject:objkey];
    [bookCache setObject:marr forKey:ky_order];
    //
    [self saveBookCache];
}
//删除一组图书
-(void)deleteBooksCacheWithBookIds:(NSArray*)bookIds ForUid:(NSInteger)uid{
    for (NSString* bookId in bookIds) {
        NSInteger bookid = [bookId integerValue];
        //移除书本下载记录 edition
        NSString* key = [NSString stringWithFormat:@"%@_%@",@(bookid),__BookDownIng];
        id obj = [bookCache objectForKey:key];
        if (obj) {
            [bookCache removeObjectForKey:key];
        }
        //移除书本已下载记录 edition
        key = [NSString stringWithFormat:@"%@",@(bookid)];
        obj = [bookCache objectForKey:key];
        if (obj) {
            [bookCache removeObjectForKey:key];
        }
        //移除书本下载顺序中记录
        NSMutableArray* marr = [[NSMutableArray alloc] initWithArray:[bookCache objectForKey:__BookDownOrderArr]];
        NSString* objkey = nil;
        for (NSString* ky in marr) {
            if(ky.length==0)continue;
            if ([key isEqual:ky]) {
                objkey = ky;
                break;
            }
        }
        if(objkey)[marr removeObject:objkey];
        [bookCache setObject:marr forKey:__BookDownOrderArr];
        //移除阅读记录
        key = [NSString stringWithFormat:@"%@_%@",@(bookid),__BookOpenRecord];
        NSString* ky = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
        obj = [bookCache objectForKey:ky];
        if (obj) {
            [bookCache removeObjectForKey:ky];
        }
        //移除用户的下载记录
        key = [NSString stringWithFormat:@"%@",@(bookid)];
        NSString* kys = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
        obj = [bookCache objectForKey:kys];
        if (obj) {
            [bookCache removeObjectForKey:kys];
        }
        //移除用的下载顺序
        NSString* ky_order = [NSString stringWithFormat:@"%d__%@",(int)uid,__BookDownOrderArr];
        marr = [[NSMutableArray alloc] initWithArray:[bookCache objectForKey:ky_order]];
        objkey = nil;
        for (NSString* ky in marr) {
            if(ky.length==0)continue;
            if ([key isEqual:ky]) {
                objkey = ky;
                break;
            }
        }
        if(objkey)[marr removeObject:objkey];
        [bookCache setObject:marr forKey:ky_order];
    }
    //
    [self saveBookCache];
}

- (void)cleanBookCache
{
    [bookCache removeAllObjects];
    [self saveBookCache];
}

- (long long)getEditionForBook:(NSInteger)bookid
{
    return [[bookCache objectForKey:[NSString stringWithFormat:@"%@",@(bookid)]] longLongValue];
}

- (long long)getEditionForDownIngBook:(NSInteger)bookid
{
    return [[bookCache objectForKey:[NSString stringWithFormat:@"%@_%@",@(bookid),__BookDownIng]] longLongValue];
}

- (NSInteger)getLastOpenPageForBook:(NSInteger)bookid forUid:(NSInteger)uid
{
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(bookid),__BookOpenRecord];
    NSString* ky = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
    return [[bookCache objectForKey:ky] integerValue];
}

//记录以及获取流式阅读器返回的JS数据
//存储
-(void) updateFlowBook:(NSInteger)bookid JSStr:(NSString*)jsStr ForUid:(NSInteger)uid{
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(bookid),__FlowBookOpenRecrod];
    NSString* ky = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
    [bookCache setObject:jsStr forKey:ky];
    [self saveBookCache];
}
//获取
-(NSString*) getFlowBookLastScrollStrWithBookId:(NSInteger)bookid ForUid:(NSInteger)uid{
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(bookid),__FlowBookOpenRecrod];
    NSString* ky = [NSString stringWithFormat:@"%d__%@",(int)uid,key];
    return [bookCache objectForKey:ky];
}
//
- (NSArray *)getAllDownBooksIDstr
{
    return [bookCache objectForKey:__BookDownOrderArr];
}

-(BOOL)isBookDownDoneWithBookId:(NSInteger)bookId{
    BOOL downDone = NO;
    NSArray* downDoneBooks = [self getAllDownBooksIDstr];
    if ([downDoneBooks containsObject:@(bookId).description]) {
        downDone = YES;
    }
    return downDone;
}

- (NSArray *)getAllDownBooksIDstrForUid:(NSInteger)uid
{
    NSString* ky_order = [NSString stringWithFormat:@"%d__%@",(int)uid,__BookDownOrderArr];
    return [bookCache objectForKey:ky_order];
}

- (NSInteger)getEditionForBook:(NSInteger)bookid forUid:(NSInteger)uid
{
    NSString* ky = [NSString stringWithFormat:@"%ld",(long)bookid];
    NSString* key = [NSString stringWithFormat:@"%d__%@",(int)uid,ky];
    return [[bookCache objectForKey:key] integerValue];
}

#pragma mark SeprateDown


//talk edition 管理
//get
-(long long) getEditionFordownloadingTalk:(NSInteger)talkid{
    return [[talkCache objectForKey:[NSString stringWithFormat:@"%@_%@",@(talkid),__TalkDownIng]] longLongValue];
}
//udate
-(void) updateTalk:(NSInteger)talkid DownEdition:(long long)edition{
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(talkid),__TalkDownIng];
    [talkCache setObject:[NSNumber numberWithLongLong:edition] forKey:key];
    [self saveTalkCache];
}
//delete
-(void)deleteTalkCache:(NSInteger)talkid{
    //移除Talk下载记录 edition
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(talkid),__TalkDownIng];
    id obj = [talkCache objectForKey:key];
    if (obj) {
        [talkCache removeObjectForKey:key];
    }
    
    [self saveTalkCache];
}


//talkVideo edition 管理
-(long long) getEditionFordownloadingTalkVideo:(NSInteger)videoid{
    return [[talkVideoCache objectForKey:[NSString stringWithFormat:@"%@_%@",@(videoid),__TalkVideoDownIng]] longLongValue];
}
//udate
-(void) updateTalkVideo:(NSInteger)videoid DownEdition:(long long)edition{
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(videoid),__TalkVideoDownIng];
    [talkVideoCache setObject:[NSNumber numberWithLongLong:edition] forKey:key];
    [self saveTalkVideoCache];
}
//delete
-(void)deleteTalkVideoCache:(NSInteger)videoid{
    //移除Talk下载记录 edition
    NSString* key = [NSString stringWithFormat:@"%@_%@",@(videoid),__TalkVideoDownIng];
    id obj = [talkVideoCache objectForKey:key];
    if (obj) {
        [talkVideoCache removeObjectForKey:key];
    }
    [self saveTalkVideoCache];
}

//批量删除cache
-(void)deleteTalkVideoCacheByIDArray:(NSArray *)videoidArray{
    for (int i = 0; i < videoidArray.count; i++) {
        NSString* key = [NSString stringWithFormat:@"%@_%@",@([videoidArray[i] integerValue]),__TalkVideoDownIng];
        id obj = [talkVideoCache objectForKey:key];
        if (obj) {
            [talkVideoCache removeObjectForKey:key];
        }
    }
    [self saveTalkVideoCache];
}


- (void)updateImageView:(UIImageView *)imgv urlStr:(NSString *)uri edition:(NSString*)uri_edition
{

}

#pragma mark Userinfo Cache
- (UserInfo *)getUserLoginInfo
{
    if(!userinfo){
        [self loadUser];
        //telen 统计
        //[UMSAgent bindUserIdentifier:[NSString stringWithFormat:@"%ld",(long)userinfo.uid]];
        //[NSObject msgToClassName:@"UMSAgent" menthed:@"bindUserIdentifier:" refs:@[[NSString stringWithFormat:@"%ld",(long)userinfo.uid]] needReturnValue:NO];
    }
    //
    return userinfo;
}

- (void)saveUserLoginInfo:(UserInfo *)info
{
    userinfo = info;
    [DataProcessing do_Async_quick:^{
        [self saveUser];
    }];
    //telen 统计
    //[UMSAgent bindUserIdentifier:[NSString stringWithFormat:@"%ld",(long)info.uid]];
    //[NSObject msgToClassName:@"UMSAgent" menthed:@"bindUserIdentifier:" refs:@[[NSString stringWithFormat:@"%ld",(long)userinfo.uid]] needReturnValue:NO];
}

- (void)saveUser {
    
    [[NSFileManager defaultManager] removeItemAtPath:[self getUserPath] error:nil];
    if(userinfo){
        @try {
            [NSKeyedArchiver archiveRootObject:userinfo toFile:[self getUserPath]];
        } @catch (NSException *exception) {
            NSLog(@"saveUser Error: %@",exception);
        } @finally {
            
        }
    }
}
-(void)loadUser {
    @try {
        userinfo = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getUserPath]];
    } @catch (NSException *exception) {
        userinfo = nil;
        NSLog(@"loadUser Error: %@",exception);
    } @finally {
        
    }
    if (!userinfo) {
        userinfo = [[UserInfo alloc] init];
    }
}
- (NSString*)getUserPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* str1 = __JRUser;
    NSString *documentsDirectory1 = [[paths firstObject] stringByAppendingPathComponent:str1];
    return documentsDirectory1;
}

#pragma mark
- (void)loaclRemindPush:(NSArray *)pushList update:(BOOL)update
{
    NSArray* list = pushList;
    if (update) {
        if(list== nil)list = @[];
        [self updateNetCache:@{@"list":list} forKey:@"loaclRemindPush"];
    }
    
    if (list == nil) {
        list = [self getNetCacheForKey:@"loaclRemindPush"][@"list"];
    }
    
    [TLRemindLocalPush cleanLastRemind];
    for (NSDictionary* dic in list) {
        NSInteger day = [dic[@"delay"] integerValue];
        NSString* msg = [DataProcessing chooseLanguageFromCN:dic[@"msgZh"] andEN:dic[@"msgEn"]];
        [TLRemindLocalPush addRemindDelay:day message:msg];
    }
}

//Device
#define Device_Login_Last @"DeviceLogin_is"
#define Device_Login_Lock @"DeviceLogin_lock"
- (BOOL)isLastLoginFromDevice
{
    NSNumber* num = [[NSUserDefaults standardUserDefaults] objectForKey:Device_Login_Last];
    if (num) {
        return [num boolValue];
    }
    return NO;
}
- (void)setLastLoginFromDevice:(BOOL)yn
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:yn] forKey:Device_Login_Last];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)isLockedDeviceLogin
{
    NSNumber* num = [[NSUserDefaults standardUserDefaults] objectForKey:Device_Login_Lock];
    if (num) {
        return [num boolValue];
    }
    return NO;
}
- (void)setLockedDeviceLogin:(BOOL)yn
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:yn] forKey:Device_Login_Lock];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)isDeviceLockUser
{
    UserInfo* uinfo = self.getUserLoginInfo;
    BOOL yn = uinfo.uid != 0;
    return (yn && [CacheData shareSingle].isLastLoginFromDevice && [CacheData shareSingle].isLockedDeviceLogin);
}
- (BOOL)isLoginUser
{
    UserInfo* uinfo = self.getUserLoginInfo;
    BOOL yn = uinfo.uid != 0;
    return (yn && ![CacheData shareSingle].isLastLoginFromDevice && ![CacheData shareSingle].isLockedDeviceLogin);
}

//无wifi下载
-(void)updateWifiStyle:(BOOL)wifiStyle{
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",@(wifiStyle)] forKey:__IsWifiStyle];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSString*)getWifiStyle{
    NSString* isWifiStyle = [[NSUserDefaults standardUserDefaults] objectForKey:__IsWifiStyle];
    return isWifiStyle;
}
//音效
-(void)updateVoiceStyle:(BOOL)voiceStyle{
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",@(voiceStyle)] forKey:__VoiceStyle];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSString*)getVoiceStyle{
    NSString* voiceStyle = [[NSUserDefaults standardUserDefaults] objectForKey:__VoiceStyle];
    return voiceStyle;
}
//疲劳模式
-(void)updateTiredStyle:(NSNumber*)tired{
    [[NSUserDefaults standardUserDefaults] setObject:tired forKey:__TiredStyle];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSNumber*)getTiredStyle{
    NSNumber* tiredStyle = [[NSUserDefaults standardUserDefaults] objectForKey:__TiredStyle];
    return tiredStyle;
}
-(void)updateTiredStyle_switchOff:(NSNumber*)tired{
    [[NSUserDefaults standardUserDefaults] setObject:tired forKey:__TiredStyle_switchOff];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSNumber*)getTiredStyle_switchOff{
    NSNumber* tiredStyle = [[NSUserDefaults standardUserDefaults] objectForKey:__TiredStyle_switchOff];
    return tiredStyle;
}

@end


